const socketIO=require('socket.io');
const server =socketIO(3000);
let dataManage=require("./data/DataManage");
let pokerService=require("./service/pokerService");
let currentRoomId=0;
let rooms={};
server.on("connection",(socket)=>{
    console.log("a user is connected")
    socket.on("add_client", function(socket){
        var clientObj = {};
        clientObj["sid"] = socket.id;
        clientObj["clientName"] = socket.client;
        var clents=[];
        //clients.push(clientObj);
        server.emit("user_status", "欢迎回家！")
    });
    //登录
    socket.on("login",(msg)=>{
        console.log(msg);
        socket.emit("loginResult","1");
    })

    // socket.on("create",(msg,socket)=>{
    //     console.log(msg);
    //     socket.emit("room","1"+socket);
    //     if(rooms.hasOwnProperty(roomNum)){
    //         rooms[roomNum].push(socket)
    //     }
    //     socket.emit("room","1"+socket);
    // })
    //创建房间
    socket.on('create',(msg)=>{

        console.log(currentRoomId);
        if(rooms.hasOwnProperty(currentRoomId)){
            socket.emit("msg","create room failture!");
        }
        else {
            console.log(currentRoomId+" room wait be created")
            let room=[];
            rooms[currentRoomId]=room;
            rooms[currentRoomId].push(msg);
            //用户加入房间
            socket.join(currentRoomId);
            console.log("NO"+rooms[currentRoomId]+"has "+rooms[currentRoomId].length+" user");
            //对房间内的用户发送消息
            server.sockets.in(currentRoomId).emit('startGame',currentRoomId);//包括自己
            currentRoomId++;
            console.log(currentRoomId);
        }
        //socket.broadcast.to(roomid).emit('event_name', data);//不包括自己
    })

    //将用户加入房间
    socket.on('join', function(roomId){
        if(rooms.hasOwnProperty(roomId)){
            if(rooms[roomId].length<=3){
                console.log(roomId+" room wait a user join")
                rooms[roomId].push(roomId)
                //用户加入房间
                socket.join(roomId);
                //对房间内的用户发送消息
                server.sockets.in(roomId).emit('startGame',roomId);//包括自己
                console.log("NO "+roomId+" has "+rooms[roomId].length+" user");
                //socket.broadcast.to(roomid).emit('event_name', data);//不包括自己
            }
            else {
                socket.emit("msg","join failture!")
            }
            if( rooms[roomId].length==3){
                console.log("prepare dispatch pokers");
                server.sockets.in(roomId).emit("dispatchPoker", data=pokerService.dispatchPoker());
            }
        }

        });
        socket.on('leave', function(data){
            if(data!=null&&data!=""){
                let roomId=data
                if(rooms.hasOwnProperty(roomId)){
                    for(let i ;i<rooms[roomId];i++){
                        data==rooms[roomId][i];
                        rooms[roomId]=rooms[roomId].splice(i,1);
                    }
                    //用户离开房间
                    socket.leave(roomid);
                    //向此用户发送信息
                    socket.emit('leavehint','you are leave！')
                }
            }

        });
    socket.on('disconnect',(msg)=>{
        console.log("a user disconnect"+msg)
    });
})
console.log('init success')